package com.pulaien.shop.controller;

import com.pulaien.common.core.controller.BaseController;
import com.pulaien.common.core.domain.AjaxResult;
import com.pulaien.system.domain.ShoppingVehicle;
import com.pulaien.system.domain.dto.ShoppingVehicleRes;
import com.pulaien.system.mapper.ShoppingVehicleMapper;
import com.pulaien.system.service.IShoppingVehicleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 购物车Controller
 *
 * @author pulaien
 */
@RestController
@RequestMapping("/shop/shoppingVehicle")
@Api(tags = "商城端购物车", description = "购物车")
public class ShopShoppingVehicleController extends BaseController {


    @Autowired
    IShoppingVehicleService shoppingVehicleService;

    @Resource
    ShoppingVehicleMapper shoppingVehicleMapper;


    @ApiOperation("商品列表")
    @GetMapping("/list")
    public AjaxResult list() {
       // Map<String,List<ShoppingVehicle>> map = shoppingVehicleService.list();

        List<Map<String,List<ShoppingVehicle>>>  list = shoppingVehicleService.list_new();

        return AjaxResult.success(list);
    }


    @ApiOperation("添加商品")
    @PostMapping("/insert")
    public AjaxResult insert(@RequestBody ShoppingVehicle shoppingVehicle) {
        return shoppingVehicleService.insert(shoppingVehicle);
    }


    @ApiOperation("更新商品数量")
    @PostMapping("/updateCount")
    public AjaxResult updateCount(@RequestBody ShoppingVehicle shoppingVehicle) {
        return shoppingVehicleService.updateCount(shoppingVehicle);
    }


    @ApiOperation("删除商品")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestParam(value = "ids") String ids) {
        return shoppingVehicleService.delete(ids);
    }



    @ApiOperation("更新购物车是否选中")
    @GetMapping("/updateSelect")
    public AjaxResult updateSelect(
            @RequestParam(value = "id") Long id,
            @RequestParam(value = "isSelected") Integer isSelected
    ) {

        ShoppingVehicle shoppingVehicle = shoppingVehicleMapper.selectById(id);
        shoppingVehicle.setIsSelected(isSelected);
        shoppingVehicleMapper.updateById(shoppingVehicle);
        return AjaxResult.success();
    }

    @ApiOperation("获得购物车商品数量")
    @GetMapping("/getCount")
    public AjaxResult getCount() {
        Integer count = shoppingVehicleService.getCount();
        return AjaxResult.success(count);
    }


    @ApiOperation("加入购物车")
    @PostMapping("/addCar")
    public AjaxResult addCar(@RequestBody ShoppingVehicleRes params) {
        return this.shoppingVehicleService.addCar(params);
    }

}
